package com.ww.grtsmanager.service.impl;

import com.ww.grtsmanager.bean.Teacher;
import com.ww.grtsmanager.mapper.TeacherMapper;
import com.ww.grtsmanager.service.TeacherService;
import com.ww.grtsmanager.vo.Conditions;
import com.ww.grtsmanager.vo.TeacherResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Override
    public void addTeacher(Teacher teacher) {
        teacher.setIsDelete(true);
        Date date = new Date();
        teacher.setGmtCreated(date);
        teacher.setGmtModified(date);
        int i = teacherMapper.addTeacher(teacher);
        if (i==0){
            throw new RuntimeException("添加讲师失败");
        }
    }

    @Override
    public TeacherResult showTeacher(Integer currentPage, Integer limit, Conditions conditions) {
        List<Teacher> list = teacherMapper.getTeacherByPage((currentPage-1)*limit,limit,conditions);
        if (list==null){
            throw new RuntimeException("您还没有录入讲师信息");
        }
        int totalCount = teacherMapper.getTeacherCount(conditions);
        if (totalCount==0){
            throw new RuntimeException("您还没有录入讲师信息");
        }
        TeacherResult result = new TeacherResult();
        result.setList(list);
        result.setTotalCount(totalCount);
        return result;
    }

    @Override
    public void deleteTeacher(Integer id) {
        int i = teacherMapper.deleteTeacher(id);
        if (i==0){
            throw new RuntimeException("删除讲师失败，请刷新页面");
        }
    }

    @Override
    public void deleteTeacherAll(List<Integer> ids) {
        if (ids.size()==0){
            throw new RuntimeException("您没有勾选需要删除的讲师");
        }
        int i = teacherMapper.deleteTeacherAll(ids);
        if (i==0){
            throw new RuntimeException("批量删除讲师失败");
        }
    }

    @Override
    public Teacher getTeacher(Integer id) {
        Teacher teacher = teacherMapper.getTeacher(id);
        if (teacher==null){
            throw new RuntimeException("讲师不存在，请刷新页面");
        }
        return teacher;
    }

    @Override
    public void updateTeacher(Teacher teacher) {
        teacher.setGmtModified(new Date());
       int i = teacherMapper.updateTeacher(teacher);
       if (i==0){
           throw new RuntimeException("修改讲师失败");
       }
    }

    @Override
    public List<Teacher> getTeacherAll() {
        List<Teacher> teacher = teacherMapper.getTeacherAll();
        return teacher;
    }
}
